package com.thinkgem.jeesite.modules.sys.listener;

import javax.servlet.ServletContextEvent;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.helper.sysinittask.ISysInitTask;

public class WebContextListener extends org.springframework.web.context.ContextLoaderListener {
	
	/**
	@Override
	public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
		if (!SystemService.printKeyLoadMessage()){
			return null;
		}
		WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		try{
			String[] taskList = Global.getConfig("systemInitTaskList").split(",");
			for(int i = 0;i< taskList.length;i++){
				String taskName = taskList[i];
				if(!StringUtils.isEmpty(taskName)){
					ISysInitTask task = (ISysInitTask) springContext.getBean(taskName);
					task.onSysInit(servletContext, springContext);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return super.initWebApplicationContext(servletContext);
	}
	*/
    /**  
     * @description 重写ContextLoaderListener的contextInitialized方法  
     */    
	@Override    
    public void contextInitialized(ServletContextEvent event) {    
        super.contextInitialized(event);    
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());    
        try{
			String[] taskList = Global.getConfig("systemInitTaskList").split(",");
			for(int i = 0;i< taskList.length;i++){
				String taskName = taskList[i];
				if(!StringUtils.isEmpty(taskName)){
					ISysInitTask task = (ISysInitTask) applicationContext.getBean(taskName);
					task.onSysInit(event.getServletContext(), applicationContext);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
    }
}
